package cn.doitedu.day01.my

import scala.io.Source

/**
 * @Date 22.3.28
 * @Created by HANGGE
 * @Description
 *          对迭代器的封装
 */
class MyRDD[T](iters:Iterator[T]) {

  def map(f: T=>T):MyRDD[T]={
    new  MyRDD[T](new  Iterator[T] {
      // 判断是否有下一个元素
      override def hasNext: Boolean = iters.hasNext
      override def next(): T = f(iters.next())
    })
  }
  def  foreach(f: T=>Unit):Unit = {
    while(iters.hasNext){
      f(iters.next())
    }
  }
}
object  MyRDD{
  def main(args: Array[String]): Unit = {
    // 迭代器  迭代文件中的数据
    val iterator = Source.fromFile("data/word.txt").getLines()
    val rdd1: MyRDD[String] = new MyRDD[String](iterator)
   /* val rdd2 = rdd1.map(x => x.asInstanceOf[Int] * 2)
    val rdd3 = rdd2.map(x => x.asInstanceOf[Int] * 3)
    val rdd4 = rdd2.map(x => x.asInstanceOf[Int] * 3)*/
  //  val rdd1: MyRDD[String] = new MyRDD[String](iterator)
    val rdd2: MyRDD[String] = rdd1.map(_.toUpperCase)
    val rdd3: MyRDD[String] = rdd2.map("hello  " + _)
    val rdd4: MyRDD[String] = rdd3.map(_.toUpperCase)
   // 调用方法   底层遍历了数据
    rdd4.foreach(println)
  }
}
